package ssn.user.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import ssn.common.util.FileUtil;
import ssn.user.service.UserService;
import ssn.user.vo.UserCareerVO;
import ssn.user.vo.UserCertVO;
import ssn.user.vo.UserVO;
import egovframework.rte.fdl.cmmn.AbstractServiceImpl;
import egovframework.rte.fdl.property.EgovPropertyService;
import egovframework.rte.fdl.string.EgovStringUtil;

/**  
 * @Class Name : 
 * @Description : 
 * 
 * @author ksh
 * @since 2013. 09.26
 * @version 1.0
 * @see
 */
@SuppressWarnings({"rawtypes","unchecked"})
@Service("userService")
public class UserServiceImpl extends AbstractServiceImpl implements UserService {
	
	@Resource(name="userDAO")
	private UserDAO userDAO; 
	
	@Resource(name = "propertiesService")
    protected EgovPropertyService propertiesService;
	
	/**
	 * 유저 정보 입력
	 */
	public void insertUserInfo(UserVO vo) throws Exception{
		if(EgovStringUtil.isNotEmpty(vo.getSau_user_pic())){
			vo.setSau_user_pic(this.userPicmove(vo));
		}
		vo.setSau_user_id(userDAO.selectUserInfoPk());
		
		userDAO.insertUserInfo(vo);
		this.insertUserCareerInfo(vo);
		this.insertUserCertInfo(vo);
	}
	
	/**
	 * 유저 경력 정보 저장
	 * @param vo
	 * @throws Exception
	 */
	public void insertUserCareerInfo(UserVO vo) throws Exception{
		int careerPk = Integer.parseInt(userDAO.selectUserCareerInfoPk(vo.getSau_user_id()));
		for(int i=0;i<vo.getUserCareerVOList().size();i++){
			vo.getUserCareerVOList().get(i).setSac_career_key(String.valueOf(careerPk+i));
		}
		userDAO.insertUserCareerInfo(vo);
	}
	
	/**
	 * 유저 자격증 정보 저장
	 * @param vo
	 * @throws Exception
	 */
	public void insertUserCertInfo(UserVO vo) throws Exception{
		int certPk = Integer.parseInt(userDAO.selectUserCertInfoPk(vo.getSau_user_id()));
		for(int i=0;i<vo.getUserCertVOList().size();i++){
			vo.getUserCertVOList().get(i).setSac_cert_key(String.valueOf(certPk+i));
		}
		userDAO.insertUserCertInfo(vo);
	}
	
	/**
	 * 업로드 사진 정보 입력
	 */
	public String userPicmove(UserVO vo){
		String realPath = propertiesService.getString("file.real.path");
		String tempPath = propertiesService.getString("file.temp.path");
		String realUrl = propertiesService.getString("file.real.url");
		return realUrl+FileUtil.moveFiles(tempPath, realPath, vo.getSau_user_pic());
	}
	
	/**
	 * 유저 정보 수정
	 */
	public void updateUserInfo(UserVO vo) throws Exception{
		String rootPath = propertiesService.getString("file.root.path");
		if(vo.getSau_user_pic().indexOf("/") == -1 && EgovStringUtil.isNotEmpty(vo.getSau_user_pic())){
			String picPath = userDAO.selectUserPicPath(vo.getSau_user_id());
			if(EgovStringUtil.isNotEmpty(picPath)){
				FileUtil.deleteFile(rootPath+userDAO.selectUserPicPath(vo.getSau_user_id()));
			}
			vo.setSau_user_pic(this.userPicmove(vo));
		}
		userDAO.updateUserInfo(vo);
		
		List<UserCareerVO> userCarrerInfoList = userDAO.selectUserCareerInfoList(vo.getSau_user_id());
		List<UserCertVO> userCertInfoList = userDAO.selectUserCertInfoList(vo.getSau_user_id());
		
		/**
		 * 경력 및 자경증 변동이 있다면 삭제후 재입력
		 */
		boolean careerModiChk = false;
		if(vo.getUserCareerVOList().size() != userCarrerInfoList.size()){
			careerModiChk = true;
		}else{
			for(int i=0;i<userCarrerInfoList.size();i++){
				UserCareerVO db = userCarrerInfoList.get(i);
				UserCareerVO paramVO = vo.getUserCareerVOList().get(i);
				if(!db.getSac_biz_period().equals(paramVO.getSac_biz_period())){
					careerModiChk = true;
					break;
				}
				if(!db.getSac_com_name().equals(paramVO.getSac_com_name())){
					careerModiChk = true;
					break;
				}
				if(!db.getSac_biz_cont().equals(paramVO.getSac_biz_cont())){
					careerModiChk = true;
					break;
				}
			}
		}
		if(careerModiChk){
			userDAO.deleteUserCareer(vo.getSau_user_id());
			this.insertUserCareerInfo(vo);
		}
		boolean certModiChk = false;
		if(vo.getUserCertVOList().size() != userCertInfoList.size()){
			certModiChk = true;
		}else{
			for(int i=0;i<userCertInfoList.size();i++){
				UserCertVO db = userCertInfoList.get(i);
				UserCertVO paramVO = vo.getUserCertVOList().get(i);
				if(!db.getSac_cert_name().equals(paramVO.getSac_cert_name())){
					certModiChk = true;
					break;
				}
				if(!db.getSac_cert_date().equals(paramVO.getSac_cert_date())){
					certModiChk = true;
					break;
				}
				if(!db.getSac_cert_publish().equals(paramVO.getSac_cert_publish())){
					certModiChk = true;
					break;
				}
			}
		}
		if(certModiChk){
			userDAO.deleteUserCert(vo.getSau_user_id());
			this.insertUserCertInfo(vo);
		}
	}
	/**
	 * 유저 정보 삭제(DB 삭제) 사용안함.
	 */
	public void deleteUserInfo(UserVO vo) throws Exception{
		userDAO.deleteUserInfo(vo);
	}
	
	/**
	 * 유저정보 리스트 및 전체 카운트
	 */
	public Map selectUserList(UserVO vo) throws Exception{
		Map resultMap = new HashMap();
		resultMap.put("resultList", userDAO.selectUserList(vo));
		resultMap.put("resultListCnt", userDAO.selectUserListCnt(vo));
		return resultMap;
	}
	/**
	 * 유저정보 조회
	 */
	public UserVO selectUserInfo(UserVO vo) throws Exception{
		return userDAO.selectUserInfo(vo);
	}
	/**
	 * 유저정보 List 조회
	 */
	public List selectUserInfoList() throws Exception{
		return userDAO.selectUserInfoList();
	}
	
	/**
	 * 사진 삭제
	 */
	public boolean deleteUserPic(String sau_user_id) {
		String rootPath = propertiesService.getString("file.root.path");
		String userPicPath = "";
		try {
			userPicPath = userDAO.selectUserPicPath(sau_user_id);
			FileUtil.deleteFile(rootPath+userPicPath);
			userDAO.deleteUserPic(sau_user_id);
		} catch (Exception e) {
			return false;
		}
		return true;
	}
}
